import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

import javax.swing.event.*;

@SuppressWarnings("serial")
public class Valemid extends JPanel implements ListSelectionListener {
	JPanel tulemus;
	JLabel proov, picture;
	JList list;
	String[] valemitenimed = { "Temperatuur", "Vool ja Pinge", "Jõud, Energia ja Töö", "Kiirus 1", "Kiirus 2",
			"Molekulaarkineetika", "Soojusmasinad", "Staatika", "Termodünaamika" };

	public Valemid() {
		picture = new JLabel();
		picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
		picture.setHorizontalAlignment(JLabel.CENTER);
		
		Arrays.sort(valemitenimed);

		list = new JList(valemitenimed);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setSelectedIndex(0);
		list.addListSelectionListener(this);
		list.setOpaque(true);
		JScrollPane listiScroll = new JScrollPane(list);

		tulemus = new JPanel();
		tulemus.setLayout(new GridLayout(1, 1));
		tulemus.add(picture);
		tulemus.setOpaque(true);
		JScrollPane tulemuseScroll = new JScrollPane(tulemus);

		JSplitPane paneel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listiScroll, tulemuseScroll);
		paneel.setDividerLocation(150);
		paneel.setEnabled(false);

		setLayout(new GridLayout(1, 1));
		add(paneel);

		Dimension minimumSize = new Dimension(100, 50);
		listiScroll.setMinimumSize(minimumSize);
		tulemuseScroll.setMinimumSize(minimumSize);

		updateLabel(valemitenimed[list.getSelectedIndex()]);
	}

	public void valueChanged(ListSelectionEvent e) { // Listi listener
		JList list = (JList) e.getSource();
		updateLabel(valemitenimed[list.getSelectedIndex()]);
	}

	protected void updateLabel(String name) {
		ImageIcon icon = Ikoonilaadija.annaIkoon("Valemid/" + name + ".jpg");
		picture.setIcon(icon);
		if (icon != null) {
			picture.setText(null);
		} else {
			picture.setText("Pilti ei leitud!");
		}
	}
}
